package com.etc.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.etc.dao.BusDao;
import com.etc.entity.Bus;
import com.etc.util.DBUtil;

public class BusDaoImpl implements BusDao{

	//实现车辆的方法
	@Override
	public boolean insertBus(Bus bus) {
		if(bus!=null) {
			String sql="insert into bus values(null,?,?,2,?)";
			return DBUtil.executeUpdate(sql, bus.getB_begin(),bus.getB_end(),bus.getM_id());	
		}	
		return false;
	}

	//实现删除方法
	@Override
	public boolean delBus(int id) {
		if(id>0) {
			String sql="delete from bus where b_id=?";
			return DBUtil.executeUpdate(sql, id);
		}
		return false;
	}

	//实现修改的方法
	@Override
	public boolean updateBus(Bus bus) {
		if(bus!=null) {
			String sql="update bus set b_begin=?,b_end=?,b_price=?,m_id=? where b_id=?";
			return DBUtil.executeUpdate(sql, bus.getB_begin(),bus.getB_end(),bus.getB_price(),bus.getM_id(),bus.getB_id());
		}
		return false;
	}

	//实现车辆查询
	@Override
	public List<Bus> queryBus() {
		String sql="select * from bus";
		CachedRowSet crs = DBUtil.execQuery(sql);
		Bus bus=null;
		List<Bus> list=new ArrayList<Bus>();
		try {
			while(crs.next()) {
				int b_id = crs.getInt(1);
				int b_begin = crs.getInt(2);
				int b_end = crs.getInt(3);
				int b_price = crs.getInt(4);
				int m_id = crs.getInt(5);
				bus = new Bus(b_id, b_begin, b_end, b_price, m_id);
				list.add(bus);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(null!=crs) {
				try {
					crs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}

}
